#!/bin/bash
root="$(cd $(dirname ${BASH_SOURCE}) && pwd)"
cd "$root"
source environment
RED=$(tput setaf 1)
RESET=$(tput sgr0)

user=${SUDO_USER:-$(whoami)}
home=$(eval echo ~$user)

function check(){
    if (($EUID==0));then
        echo "Don't need run as root."
        exit 1
    fi
}

function usage(){
    cat <<-EOF
	Usage: $(basename $0) CMD
	CMD:
    	install
    	uninstall
	EOF
    exit 1
}

function installAutoconf(){
    cd "$root"
    echo "installAutoconf()"
    # curl -O -L http://ftpmirror.gnu.org/autoconf/autoconf-2.69.tar.gz
    cp autoconf-2.69.tar.gz /tmp
    cd /tmp
    tar xvf autoconf-2.69*
    cd autoconf-2.69*
    ./configure
    make
    sudo make install
    cd "$root"
}

function installPrivoxyFromSource(){
    cd "$root"
    echo "installPrivoxyFromSource()"
    if ! command -v autoconf >/dev/null 2>&1;then
        installAutoconf
    fi

    cp privoxy-3.0.24*tar.gz /tmp
    # curl -LO https://svwh.dl.sourceforge.net/project/ijbswa/Sources/3.0.24%20%28stable%29/privoxy-3.0.24-stable-src.tar.gz
    cd /tmp
    tar xvf privoxy-3.0*
    cd privoxy-3.0*
    autoheader
    autoconf
    ./configure
    make
    #MacOS needed
    sudo make install  USER=${user} GROUP=staff
    sudo ln -sf /usr/local/sbin/privoxy /usr/local/bin
    cd "$root"
}

function install(){
    cd "$root"
    echo "install()"
    check
    if ! command -v privoxy >/dev/null 2>&1;then
        #install privoxy
        if command -v apt-get >/dev/null 2>&1;then
            sudo apt-get install -y privoxy
        elif command -v yum >/dev/null 2>&1;then
            sudo yum install -y privoxy
        elif command -v pacman >/dev/null 2>&1;then
            sudo pacman -S privoxy --noconfirm
        elif [ "$(uname)" = "Darwin" ];then
            # if ! command -v brew >/dev/null 2>&1;then
            #     echo "On MacOS,need homebrew installed."
            #     exit 1
            # fi
            # echo "Using proxy (socks5://localhost:1080) to use brew ? [Y/n] " useP
            # read useP
            # if [[ "$useP" != [nN] ]];then
            #     export ALL_PROXY=socks5://localhost:1080
            # fi
            # brew install privoxy
            # pri=$(brew list privoxy | grep '/sbin/privoxy')
            # if [ -z "$pri" ];then
            #     echo "Can't find privoxy,install failed?"
            #     exit 1
            # fi
            # ln -sf "$pri" /usr/local/bin
            installPrivoxyFromSource
        fi
    fi
    if ! command -v privoxy >/dev/null 2>&1;then
        echo "Please install privoxy manually."
        exit 1
    fi

    case $(uname) in
        Linux)
            cmds=$(cat<<-EOF
			sed -e "s|ROOT|$root|g" -e "s|PRIVOXY|$(which privoxy)|g" -e "s|USER|$user|g" Linux/${PAC_PRIVOXY}.service >/etc/systemd/system/${PAC_PRIVOXY}.service
			sed -e "s|ROOT|$root|g" -e "s|PRIVOXY|$(which privoxy)|g" -e "s|USER|$user|g" Linux/${GLOBAL_PRIVOXY}.service >/etc/systemd/system/${GLOBAL_PRIVOXY}.service
			systemctl daemon-reload
			systemctl enable $PAC_PRIVOXY
			systemctl enable $GLOBAL_PRIVOXY
			ln -sf $root/pctl /usr/local/bin
			EOF
            )
            sudo -- sh -c "$cmds"
            echo "run ${RED}pctl makeConf${RESET} to generate config file,then start"
            ;;
        Darwin)
            if [ ! -d "$home/Library/LaunchAgents" ];then
                mkdir -p "$home/Library/LaunchAgents"
            fi
            #privoxy plist
            # sbin=$(brew list privoxy | grep '/sbin/privoxy')
            sbin=$(which privoxy)
            sed -e "s|ROOT|$root|g" -e "s|SBIN|$sbin|g" "$root/Darwin/global.privoxy.plist" > "$home/Library/LaunchAgents/global.privoxy.plist"
            sed -e "s|ROOT|$root|g" -e "s|SBIN|$sbin|g" "$root/Darwin/pac.privoxy.plist" > "$home/Library/LaunchAgents/pac.privoxy.plist"
            sudo ln -sf $root/pctl /usr/local/bin
            ;;
    esac
    cd "$root"
}

function uninstall(){
    cd "$root"
    echo "uninstall()"
    case $(uname) in
        Linux)
            cmds=$(cat<<-EOF
				systemctl stop $PAC_PRIVOXY
				systemctl stop $GLOBAL_PRIVOXY
				systemctl disable $PAC_PRIVOXY
				systemctl disable $GLOBAL_PRIVOXY
				rm /etc/systemd/system/$PAC_PRIVOXY.service
				rm /etc/systemd/system/$GLOBAL_PRIVOXY.service
				rm /usr/local/bin/pctl
				EOF
            )
            sudo -- sh -c "$cmds"
            ;;
        Darwin)
            launchctl unload $home/Library/LaunchAgents/global.privoxy.plist
            launchctl unload $home/Library/LaunchAgents/pac.privoxy.plist
            sudo rm /usr/local/bin/pctl
            ;;
    esac
    cd "$root"
}

cmd=$1
case $cmd in
    install)
        install
        ;;
    uninstall)
        uninstall
        ;;
    *)
        usage
        ;;
esac

